package com.lemon.smartsafetynew.ui.adapter.hidden;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenBo;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.net.services.CommonServices;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.lemon.smartsafetynew.util.hidden.SafeHiddenUtils;

import java.util.Date;
import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/7.
 */
public class SafeHiddenListAdapter extends LemonRcvAdapter<SafeHiddenDownloadVo> {

    private String status = "";
    private RecyclerTClickListener<SafeHiddenDownloadVo> mListener;

    public SafeHiddenListAdapter(@Nullable List<SafeHiddenDownloadVo> data,
                                 RecyclerTClickListener<SafeHiddenDownloadVo> mListener,
                                 String status) {
        super(data);
        this.mListener = mListener;
        this.status = status;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    private class ItemAdapter implements AdapterItem<SafeHiddenDownloadVo> {

        private SafeHiddenDownloadVo mData;
        private int mPosition;

        private TextView hiddenDangerCodeNameTv;
        private TextView hiddenDangerCodeItemTv;
        private LinearLayout hiddenDangerCodeLayout;

        private TextView companyItemNameTv;
        private TextView companyItemTv;
        private LinearLayout companyItemLayout;

        private TextView safeHiddenItemNameTv;
        private TextView safeHiddenItemTv;
        private LinearLayout safeHiddenItemLayout;

        private TextView safeHiddenDepartmentNameTv;
        private TextView safeHiddenDepartmentTv;
        private LinearLayout safeHiddenDepartmentLayout;

        private TextView safeHiddenPersonNameTv;
        private TextView safeHiddenPersonTv;
        private LinearLayout safeHiddenPersonLayout;

        private TextView safeHiddenCloseTimeNameTv;
        private TextView safeHiddenCloseTimeTv;
        private LinearLayout safeHiddenCloseTimeLayout;

        private ImageView rightTopImageView;
        private LinearLayout safeHiddenLayout;
        private ImageView rightBottomImageView;
        private ImageView chooseImageView;

        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_hidden;
        }

        @Override
        public void bindViews(View root) {

            hiddenDangerCodeNameTv = (TextView) root.findViewById(R.id.hidden_danger_code_tv);
            hiddenDangerCodeItemTv = (TextView) root.findViewById(R.id.hidden_danger_code_item_tv);
            hiddenDangerCodeLayout = (LinearLayout) root.findViewById(R.id.hidden_danger_code_layout);

            companyItemNameTv = (TextView) root.findViewById(R.id.company_item_name_tv);
            companyItemTv = (TextView) root.findViewById(R.id.company_item_tv);
            companyItemLayout = (LinearLayout) root.findViewById(R.id.company_layout);

            safeHiddenItemNameTv = (TextView) root.findViewById(R.id.safe_hidden_item_name_tv);
            safeHiddenItemTv = (TextView) root.findViewById(R.id.safe_hidden_item_tv);
            safeHiddenItemLayout = (LinearLayout) root.findViewById(R.id.safe_hidden_item_layout);

            safeHiddenDepartmentNameTv = (TextView) root.findViewById(R.id.safe_hidden_department_name_tv);
            safeHiddenDepartmentTv = (TextView) root.findViewById(R.id.safe_hidden_department_tv);
            safeHiddenDepartmentLayout = (LinearLayout) root.findViewById(R.id.safe_hidden_department_layout);

            safeHiddenPersonNameTv = (TextView) root.findViewById(R.id.safe_hidden_person_name_tv);
            safeHiddenPersonTv = (TextView) root.findViewById(R.id.safe_hidden_person_tv);
            safeHiddenPersonLayout = (LinearLayout) root.findViewById(R.id.safe_hidden_person_layout);

            safeHiddenCloseTimeNameTv = (TextView) root.findViewById(R.id.safe_hidden_close_time_name_tv);
            safeHiddenCloseTimeTv = (TextView) root.findViewById(R.id.safe_hidden_close_time_tv);
            safeHiddenCloseTimeLayout = (LinearLayout) root.findViewById(R.id.safe_hidden_close_time_layout);

            rightTopImageView = (ImageView) root.findViewById(R.id.right_top_imageView);
            rightBottomImageView = (ImageView) root.findViewById(R.id.right_bottom_imageView);
            chooseImageView = (ImageView) root.findViewById(R.id.choose_imageView);

            safeHiddenLayout = (LinearLayout) root.findViewById(R.id.safe_hidden_layout);
        }

        @Override
        public void setViews() {
            safeHiddenLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mListener.onItemClick(mData, mPosition);
                    //TODO
                    //chooseImageView.setSelected(!chooseImageView.isSelected());

                }
            });
        }

        @Override
        public void handleData(SafeHiddenDownloadVo safeHiddenDownloadVo, int position) {

            mData = safeHiddenDownloadVo;
            mPosition = position;

//            if(SafeHiddenBo.Report.report.getValue().equals(safeHiddenDownloadVo.getUp_to_official())) {
//                rightTopImageView.setVisibility(View.VISIBLE);
//            } else {
//                rightTopImageView.setVisibility(View.GONE);
//            }

            if (SafeHiddenBo.Status.overtime_rectify.getValue().equals(safeHiddenDownloadVo.getStatus())
                    || SafeHiddenBo.Status.rectify.getValue().equals(safeHiddenDownloadVo.getStatus())) {        // 超期未整改 或者 待整改

                safeHiddenCloseTimeLayout.setVisibility(View.GONE);

                hiddenDangerCodeItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getCode()));
                companyItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getCompany_name()));
                safeHiddenItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getName()));
                safeHiddenDepartmentNameTv.setText("隐患来源：");
                safeHiddenDepartmentTv.setText(CommonUtil.avoidNullString(SafeHiddenUtils.getSourceType(safeHiddenDownloadVo.getSource_type())));
                safeHiddenPersonNameTv.setText("当前处理人：");
                safeHiddenPersonTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getRectify_user_name()));

            } else if (SafeHiddenBo.Status.review.getValue().equals(safeHiddenDownloadVo.getStatus())) {  //待复查

                safeHiddenCloseTimeLayout.setVisibility(View.GONE);

                hiddenDangerCodeItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getCode()));
                companyItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getCompany_name()));
                safeHiddenItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getName()));
                safeHiddenDepartmentNameTv.setText("隐患来源：");
                safeHiddenDepartmentTv.setText(CommonUtil.avoidNullString(SafeHiddenUtils.getSourceType(safeHiddenDownloadVo.getSource_type())));
                safeHiddenPersonNameTv.setText("当前处理人：");
                safeHiddenPersonTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReview_user_name()));

            } else if (SafeHiddenBo.Status.finish.getValue().equals(safeHiddenDownloadVo.getStatus())) {  //结束（已关闭--全部）

                safeHiddenCloseTimeLayout.setVisibility(View.VISIBLE);

                hiddenDangerCodeItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getCode()));
                companyItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getCompany_name()));
                safeHiddenItemTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getName()));
                safeHiddenDepartmentNameTv.setText("隐患来源：");
                safeHiddenDepartmentTv.setText(CommonUtil.avoidNullString(SafeHiddenUtils.getSourceType(safeHiddenDownloadVo.getSource_type())));
                safeHiddenPersonNameTv.setText("复查人员：");
                safeHiddenPersonTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReview_user_name()));
                safeHiddenCloseTimeNameTv.setText("关闭时间：");
                safeHiddenCloseTimeTv.setText(CommonUtil.secondToTimeStr(safeHiddenDownloadVo.getReview_time(), DateTypeEnum.DAY));
            }

        }
    }
}
